package com.springboot.securtiy.handler;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
* Description: 自定义403处理异常
* date: 2023/12/4 15:07<br/>
* @author: zhangzaijun <br/>
* @since JDK 1.8
*/

@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
        httpServletResponse.setHeader("Content-Type","application/json;charset=utf-8");
        PrintWriter pr = httpServletResponse.getWriter();
        pr.write("{\"status\":\"error\",\"msg\":\"权限不足，请联系管理 员!\"}");
        pr.flush();
        pr.close();
    }
}
